using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UFrame.Audio;
using System;

public class BundleAudioLoader : AudioClipLoader
{
    protected HashSet<string> m_loadedBundles = new HashSet<string>();

    protected void LoadAudioFromPath(string path, Action<AudioClip> onLoad)
    {
        var assetBundleName = path.ToLower();
        var assetName = System.IO.Path.GetFileNameWithoutExtension(path);
        AssetBundleAgent.Instance.LoadAssetAsync(assetBundleName + ".ab", assetName, onLoad,null,false);
    }

    protected override void LoadAudioClip(object audioId, bool isSfx, Action<AudioClip> onLoad)
    {
        Debug.Log("start bundle load audio:" + audioId);
        if (audioId is int)
        {
            var audioCfg = TableAgent.Instance.GetSoundCfgById((int)audioId);
            if (audioCfg != null)
            {
                LoadAudioFromPath(audioCfg.path, onLoad);
            }
        }
        else
        {
            LoadAudioFromPath(audioId.ToString(), onLoad);
        }

    }

    public override void ClearAllLoaded()
    {
        base.ClearAllLoaded();
        foreach (var bundleName in m_loadedBundles)
        {
            AssetBundleAgent.Instance.UnloadAssetBundle(bundleName, true);
        }
    }
}